<template>
  <GapedColumn>
    <AddButton buttonText="新增业务" @click="addBusinessDialog.show()" />
    <el-table :data="computedBusinessItems" style="width: 100%">
      <el-table-column prop="name" label="业务名称" />
      <el-table-column label="业务Topo分区" width="180">
        <template #default="scope">
          <TagList v-if="scope.row.sections" :items="scope.row.sections" />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="业务集群" width="180">
        <template #default="scope">
          <TagList v-if="scope.row.clusters" :items="scope.row.clusters" />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fiexed width="260">
        <template #default="scope">
          <EditButton @click="addBusinessDialog.show({ ...scope.row })" />
          <DeleteButton @click="removeBusiness(scope.$index)" />
          <el-button
            type="primary"
            size="small"
            @click="$emit('click', scope.row.name)"
          >
            配置拓扑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </GapedColumn>
  <AddBusinessDialog ref="addBusinessDialog" :saveCallback="handleSave" />
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import useApi from '@/core/apiStore'
import { chain } from 'lodash'
import DeleteButton from '@/components/DeleteButton.vue'
import EditButton from '@/components/EditButton.vue'
import AddButton from '@/components/AddButton.vue'
import { GapedColumn } from '@/core/components'
import AddBusinessDialog from './AddBusinessDialog.vue'
import TagList from '@/components/TagList.vue'
import {
  showAlert,
  checkDuplicateCondition,
  showSuccessSaveMessage,
  showConfirmDelete,
} from '@/utils'

const apiStore = useApi()

onMounted(() => {
  apiStore.getBusinessItems()
})
const addBusinessDialog = ref(null)
const computedBusinessItems = computed(() => {
  return chain(apiStore.apiStates.getBusinessItems.data)
    .map((item, index) => {
      return {
        ...item,
        id: index,
      }
    })
    .value()
})

const saveBusinessItems = async () => {
  await apiStore.setBusinessItems({
    items: apiStore.apiStates.getBusinessItems.data,
  })
  showSuccessSaveMessage()
}

const handleSave = async businessData => {
  const isDuplicate = checkDuplicateCondition(
    computedBusinessItems.value,
    businessData
  )

  if (isDuplicate) {
    showAlert('业务名称已存在，请使用其他名称')
    return false
  }

  // 如果是编辑操作，更新现有业务
  if (businessData.id !== undefined) {
    const index = computedBusinessItems.value.findIndex(
      b => b.id === businessData.id
    )
    if (index !== -1) {
      apiStore.apiStates.getBusinessItems.data[index] = { ...businessData }
    }
  } else {
    // 新增业务
    apiStore.apiStates.getBusinessItems.data.push({
      name: businessData.name,
      sections: businessData.sections,
      clusters: businessData.clusters,
    })
  }

  await saveBusinessItems()
  return true
}

const removeBusiness = async (index: number) => {
  const name = apiStore.apiStates.getBusinessItems.data[index]?.name
  await showConfirmDelete(name, async () => {
    apiStore.apiStates.getBusinessItems.data.splice(index, 1)
    await saveBusinessItems()
  })
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  gap: 10px;
}
</style>
